package java8;

import spring.model.User;

import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Function;

public class LambdaUtil {

    public static void main(String[] args) throws Exception {
        test();
    }

    public static void runnable() {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("run");
            }
        };
        r1.run();

        Runnable r = () -> {
            System.out.println("run");
        };
        r.run();

        Runnable r2 = () -> System.out.println("run");
        r2.run();
    }

    public static void callable() throws Exception {
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "hello";
            }
        };
        try {
            callable.call();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(callable.call());


        Callable<String> callable1 = () -> {
            return "hello1";
        };
        callable1.call();
        System.out.println(callable1.call());

        Callable<String> callable2 = () -> "hello2";
        System.out.println(callable2.call());
    }

    public static void insertUser() {
        UserMapper userMapper = new UserMapper() {
            @Override
            public void insert(User user) {
                System.out.println("insert user:" + user);
            }
        };
        userMapper.insert(new User());

        UserMapper userMapper2 = (User user) -> {
            System.out.println("insert user:" + user);
        };
        userMapper2.insert(new User());

        UserMapper userMapper3 = (User user) -> System.out.println("insert user:" + user);
        userMapper3.insert(new User());

        UserMapper userMapper4 = (user) -> System.out.println("insert user:" + user);
        userMapper4.insert(new User());

        OrderMapper orderMapper = new OrderMapper() {
            @Override
            public int insert(User user) {
                return 1;
            }
        };
        OrderMapper orderMapper1 = (user) -> 1;
        OrderMapper orderMapper2 = (user) -> {
            return 1;
        };
        OrderMapper orderMapper3 = (User user) -> {
            return 1;
        };
        System.out.println(orderMapper1.insert(new User()));
        System.out.println(orderMapper2.insert(new User()));
        System.out.println(orderMapper3.insert(new User()));

        Function<Integer, Integer> function = (a) -> {
            int sum = 0;
            for (int i = 1; i < a; i++) {
                sum += 1;
            }
            return sum;
        };
        System.out.println(function.apply(10));

        BiFunction<Integer,Integer,String> biFunction = (a,b) -> {
            return "String";
        };


    }

    static int get() {
        System.out.println("get");
        return 1;
    }

    static String find() {
        System.out.println("find");
        return "find";
    }

    static void exec() {
        System.out.println("exec");
    }

    public static void test (){
        Runnable runnable = () -> get();
        runnable.run();
        Runnable runnable1 = () -> exec();
        runnable1.run();
        Runnable runnable2 = () -> find();
        runnable2.run();

        Fun fun = () -> get();
        fun.fun();

        // 方法返回值类型必须和函数返回值一致。
//        Fun fun1 = () -> find();
//        Fun fun2 = () -> exec();

    }


}


interface UserMapper {
    void insert(User user);
}

interface OrderMapper {
    int insert(User user);
}

//只有一个抽象方法（Object类的方法除外）的接口是函数式接口
interface Fun {
    int fun();

    // toString是Object的方法
    String toString();

    // 已实现的方法
    static String getFuns(){
      return "fun";
    };

}
